package com.chrilwe.pigpig.auth.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Base64Utils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.chrilwe.pigpig.auth.common.request.LoginRequest;
import com.chrilwe.pigpig.auth.common.request.PhoneLoginRequest;
import com.chrilwe.pigpig.auth.common.response.LoginResult;
import com.chrilwe.pigpig.auth.service.AuthService;
import com.chrilwe.pigpig.auth.service.PhoneService;

import cn.chrilwe.pigpig.core.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author chrilwe
 * 2020-8-23
 */
@RestController
@RequestMapping("/auth")
@Api(tags = "认证中心接口")
public class AuthController extends BaseController {
	
	@Autowired
	protected AuthService authService;
	
	@Autowired
	protected PhoneService phoneService;

	@PostMapping("/passwordLogin")
	@ApiOperation("密码登录方式")
	public LoginResult passwordLogin(@RequestBody LoginRequest loginRequest) {
		
		return authService.doLogin(loginRequest);
	}
	
	@PostMapping("/phoneLogin")
	@ApiOperation("手机号码登录方式")
	@ApiImplicitParam
	public LoginResult phoneLogin(@RequestBody PhoneLoginRequest phoneRequest) {
		
		return null;
	}

	
	@Autowired
	RestTemplate restTemplate;
	@GetMapping("/token")
	public Map test() {
		String target = "XcWebApp:XcWebApp";
		byte[] encode = Base64Utils.encode(target.getBytes());

		LinkedMultiValueMap<String, String> header = new LinkedMultiValueMap<String, String>();
		header.add("Authorization", "Basic " + new String(encode));
		LinkedMultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();
		body.add("grant_type", "password");
		body.add("username", "chrilwe");
		body.add("password", "123456");
		HttpEntity entity = new HttpEntity(body, header);
		ResponseEntity<Map> responseEntity = restTemplate.exchange("http://localhost:9003/oauth/token", HttpMethod.POST, entity, Map.class);
		Map res = responseEntity.getBody();
		return res;
	}
}
